package com.example.day_day_up.servlet;

import com.example.day_day_up.entity.constants.Constants;
import com.example.day_day_up.entity.vo.OnlineExamVO;
import com.example.day_day_up.service.ExamService;
import com.example.day_day_up.utils.JsonUtils;


import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/exam")
public class ExamServlet extends HttpServlet {
    private ExamService examService;

    @Override
    public void init() throws ServletException {
        super.init();
        ServletContext servletContext = getServletContext();
        examService = (ExamService) servletContext.getAttribute("examService");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String id = req.getParameter("id");
        // TODO
        // 查询到考试信息， 单选题 判断题 多选题
        HttpSession session = req.getSession();
        OnlineExamVO examDTO = (OnlineExamVO) session.getAttribute(Constants.EXAM_KEY + id);
        if (examDTO != null) {
            JsonUtils.writeSuccessResponse(resp, examDTO);
            return;
        }
        examDTO = examService.getExamById(id);
        session.setAttribute(Constants.EXAM_KEY + id, examDTO);

        JsonUtils.writeSuccessResponse(resp, examDTO);
    }
}

